package com.xiaodou.mjf.application.core.service.impl;

import com.xiaodou.mjf.application.core.repository.InvestRepository;
import com.xiaodou.mjf.application.core.service.InvestService;
import com.xiaodou.mjf.entity.core.Financing;
import com.xiaodou.mjf.entity.core.Invest;
import com.xiaodou.mjf.infrastructure.common.app.JpaSimpleService;
import com.xiaodou.mjf.infrastructure.common.entity.DataEntity;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.persistence.criteria.Predicate;
import javax.transaction.Transactional;

import java.util.Date;
import java.util.List;

/***
 *
 * Invest业务访问实现
 * 
 * @author XiaoDou.com Code Generator
 * @version 1.0 2014-10-29 17:37:52
 *
 */
@Service
@Transactional
public class InvestServiceImpl extends JpaSimpleService<Invest, String> implements InvestService {

	Logger logger = LoggerFactory.getLogger(InvestServiceImpl.class);

	@Resource
	InvestRepository investRepository;

	@Override
	protected JpaRepository<Invest, String> getRepository() {
		return investRepository;
	}

	@Override
	public Page<Invest> page(Invest entity) {

		return investRepository.findAll((root, query, cb) -> {

			Predicate p1 = cb.equal(root.get("term").as(String.class), entity.getTerm());

			return query.where(p1).getRestriction();

		}, preparePage(entity, new Sort(Direction.DESC, "investDate")));

	}

	@Override
	public Page<Invest> pageAll(Invest entity) {

		return investRepository.findAll((root, query, cb) -> {

			Predicate p1 = cb.equal(root.get("memberId").as(String.class), entity.getMemberId());
			Predicate p2 = cb.equal(root.get("type").as(String.class), entity.getType());
			Predicate p3 = cb.equal(root.get("status").as(String.class), DataEntity.NORMAL);

			return query.where(p1, p2, p3).getRestriction();

		}, preparePage(entity, new Sort(Direction.DESC, "investDate")));

	}

	@Override
	public Page<Invest> pageForPerson(Invest entity) {

		return investRepository.findAll((root, query, cb) -> {

			Predicate p1 = cb.equal(root.get("term").as(Integer.class), entity.getTerm());
			Predicate p2 = cb.equal(root.get("type").as(Integer.class), entity.getType());
			Predicate p3 = cb.equal(root.get("paymentStatus").as(Integer.class), entity.getPaymentStatus());

			return query.where(p1, p2, p3).getRestriction();

		}, preparePage(entity, new Sort(Direction.DESC, "investDate")));

	}

	public Integer count(Integer term, Integer financeType) {
		Long count = investRepository.count((root, query, cb) -> {
			Predicate p1 = cb.equal(root.get("term").as(Integer.class), term);
			Predicate p2 = cb.equal(root.get("type").as(Integer.class), financeType);
			Predicate p3 = cb.equal(root.get("financeStatus").as(Integer.class), Financing.INVEST_STATUS_REPAYMENT);
			Predicate p4 = cb.equal(root.get("financeStatus").as(Integer.class), Financing.INVEST_STATUS_PAID);
			return query.where(p1, p2, p3, p4).getRestriction();
		});
		return count == null ? 0 : count.intValue();
	}

	public Integer countPerson(Integer term, Integer financeType) {
		Long count = investRepository.count((root, query, cb) -> {
			Predicate p1 = cb.equal(root.get("term").as(Integer.class), term);
			Predicate p2 = cb.equal(root.get("type").as(Integer.class), financeType);
			Predicate p3 = cb.equal(root.get("paymentStatus").as(Integer.class), Invest.INVEST_PAYMENT);
			return query.where(p1, p2, p3).getRestriction();
		});
		return count == null ? 0 : count.intValue();
	}

	@Override
	public Double getCumulativeAmountBymemberId(String memberId, int type) {
		return investRepository.getCumulativeAmountBymemberId(memberId, type);
	}

	@Override
	public Double getCumulativeProfitBymemberId(String memberId, int type) {
		return investRepository.getCumulativeProfitBymemberId(memberId, type);
	}

	@Override
	public List<Invest> findByFinanceId(String financeId) {
		return investRepository.findByFinanceId(financeId);
	}

	@Override
	public Object[][] proBydateTotal(Integer type, Date beginDate, Date endDate) {
		return investRepository.proBydateTotal(type, beginDate, endDate);
	}

	@Override
	public List<Invest> cusPurchaseRecord(String memberId) {
		return investRepository.cusPurchaseRecord(memberId);
	}

	@Override
	public List<Invest> findByPaymentStatusAndStatus(Integer paymentStatus, Integer status) {
		return investRepository.findByPaymentStatusAndStatus(paymentStatus, status);
	}

	@Override
	public List<Invest> findForNotPayment(String financeId, String memberId,
			Integer paymentStatus) {
			return investRepository.findForNotPayment(financeId, memberId,1,0);			
	}

	@Override
	public List<Invest> findForNotPaymentXiaodou(String financeId,
			String memberId, Integer paymentStatus, Double amount) {
		
		return investRepository.findForNotPaymentXiaoDou(financeId, memberId, 1, 0,amount);	
	}
}
